/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.runtime.component
{
	import flash.events.FocusEvent;
	/**
	 * 货币输入控件。 
	 * 
	 */	
	public class EDICurrencyTextInput extends EDITextInput
	{
		import mx.formatters.CurrencyFormatter;
		
		private var _cf:CurrencyFormatter; 
		private var _text:String=""
		private var setText:Boolean=false;
		
		public function EDICurrencyTextInput()
		{
			super(); 
			  
			_cf=new CurrencyFormatter();
			_cf.precision=2;
	        _cf.rounding="none" 
	        _cf.decimalSeparatorTo="."  
	        _cf.thousandsSeparatorTo=","
	        _cf.useThousandsSeparator=true  
	        _cf.useNegativeSign=true
	        _cf.currencySymbol="￥" 
	        _cf.alignSymbol="left";
			
			restrict="-0-9\.";
			verify=/^(\-|\+)?\d+(\.\d+)?$/;
			//text="0"; 
		}
		
		override public function set text(value:String):void
		{
			_text=value;
			EDIApplication.outputDebug("CurrencyInput: "+value);
			super.text=	_cf.format(value);
			setText=true;
		}
		
		override public function get text():String
		{
			return _text;
		}
		
		/**
		 * 设置货币格式化 
		 * @return 
		 * 
		 */		
		public function get currencyFormat():CurrencyFormatter
		{
			return _cf;
		}
		
		override protected function focusInHandler(event:FocusEvent):void
		{
			if(editable)
			{
				super.text=_text=="0"?"":_text;
				setText=false;
			}
		}
		
		override protected function focusOutHandler(event:FocusEvent):void
		{
			if(editable)
			{
				_text=setText?_text:(super.text==""?"0":super.text);
				super.text=_cf.format(_text);
				setText=false;
			}
		}
		
		/**
		 * 检查是否符合要求。 
		 * @return 
		 * 
		 */		
		override public function check():Boolean
		{
			var ret:Boolean=false;
			if(!notNull)
			{
				if(text=="")
				{
					return ret;
				}
			}
			
			if(fieldLength>0)
			{
				var length:int=text.replace(/[^\x00-\xff]/g,"xx").length;
				if(fieldLength<length)
				{
					return ret;
				}
			}			
			
			if(verify!=null)
			{
				if(!verify.test(text))
				{
					return ret;
				}				
			}
			
			return true;
		}
	}
}